﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using 配置;
using static System.Formats.Asn1.AsnWriter;

namespace ConsoleApp6
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ServiceCollection services = new ServiceCollection();
            services.AddTransient(typeof(PrintService));
            
            ConfigurationBuilder configBuilder = new ConfigurationBuilder();
            configBuilder.AddJsonFile("setting.json", false, true);
            var configRoot = configBuilder.Build();

            // 通过键值对读
            var name = configRoot["name"];
            Console.WriteLine(name);

            // 通过路径读取配置
            var workName = configRoot.GetSection("work:address");
            Console.WriteLine(workName.Value);

            // 转换成对象
            var emp = configRoot.Get<Employee>();
            Console.WriteLine(emp.ToString());

            //选项方式
            services.AddSingleton<IConfiguration>(configRoot);
            services.AddOptions<Employee>().Configure<IConfiguration>((emp, config) =>
            {
                config.Bind(emp);
            });
            using (var sp = services.BuildServiceProvider())
            {
                
                while (true)
                {
                    using (var scop = sp.CreateScope())
                    {
                        var print = scop.ServiceProvider.GetRequiredService<PrintService>();
                        print.Print();
                        Console.ReadKey();
                        print.Print();
                        Console.WriteLine("***********************");
                        Console.ReadKey();
                    }
                }
            }
        }
    }
}